﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gift.Utile;
using Gift.Model;
using Gift.DAL;
using System.Web.Security;

namespace WebSite.admin.company
{
    public partial class CompanyManager : BaseAdmin
    {
        readonly dalCompany theCompany = new dalCompany();
        readonly adminlogin theAdminLogin = new adminlogin();
        public int ParaCompanyId
        {
            get {
                if (string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    return -1;
                }
                else
                {
                    return int.Parse(Request.QueryString["id"]);
                }
            }
        }
        public string Action
        {
            get
            {
                return Request.QueryString["action"];
               
            }
        }


        public void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(Gift.Model.ENUM.ERule.AddCompany);
                this.BuildForm();
            }
        }

        void BuildForm()
        {
            if (Action == "del")
            {
                theCompany.Delete(ParaCompanyId);
                base.Response.Write("<script language='javascript'>alert('删除成功');window.location.href='companyList.aspx';</script>");
            }

            else if (ParaCompanyId > 0)
            {
                tbCompany oCompany;
                oCompany = theCompany.GetModel(ParaCompanyId);
                if (theCompany != null)
                {
                    this.txtAddress.Text = oCompany.Address;
                    this.txtEmail.Text = oCompany.Email;
                    this.txtFax.Text = oCompany.Fax;
                    this.txtLinker.Text = oCompany.Linker;
                    this.txtName.Text = oCompany.Name;
                    this.hdCompany.Value = oCompany.Name;
                    this.txtPostcode.Text = oCompany.Postcode;
                    this.txtQQ.Text = oCompany.QQ;
                    this.txtTel.Text = oCompany.Telephone;
                    this.ddlCompanyType.Items.FindByValue(oCompany.CompanyTypeId.ToString()).Selected = true;

                    DB_adminlogin oAdminLogin;
                    oAdminLogin = theAdminLogin.GetModel(oCompany.AdminId);
                    if (oAdminLogin != null)
                    {
                        this.txtUserName.Text = oAdminLogin.AdminUser;
                        this.hdLoginUser.Value = this.txtUserName.Text.Trim();
                     
                    }
                }
            }
        }

        public void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            if ((this.txtUserName.Text.Trim() == "") || this.txtPassword.Text.Trim() == "" || this.txtEmail.Text.Trim() == "" || this.txtName.Text.Trim() =="" )
            {
                base.Response.Write("<script language='javascript'>alert('登录名称、密码、电子邮件、公司名称不能为空！');window.location.href='companyManager.aspx';</script>");
                base.Response.End();
            }

            tbCompany oCompany;
            DB_adminlogin oAdminLogin;
            oCompany = new tbCompany();
            oCompany.Id = ParaCompanyId;
            oCompany.Address = this.txtAddress.Text.Trim();
            oCompany.Email = this.txtEmail.Text.Trim();
            oCompany.Linker = this.txtLinker.Text.Trim();
            oCompany.Fax = this.txtFax.Text.Trim();
            oCompany.Name = this.txtName.Text.Trim();
            oCompany.Postcode = this.txtPostcode.Text.Trim();
            oCompany.QQ = this.txtQQ.Text.Trim();
            oCompany.Telephone = this.txtTel.Text.Trim();
            oCompany.CompanyTypeId = int.Parse(this.ddlCompanyType.SelectedItem.Value);

            try
            {
                if (ParaCompanyId > 0)
                {
                    if (this.hdCompany.Value != oCompany.Name)
                    {
                        tbCompany tempCompany = theCompany.GetModelByName(oCompany.Name);
                        if (theCompany != null)
                        {
                            base.Response.Write("<script language='javascript'>alert('该公司名已存在,请重新输入公司名!');</script>");
                            return;
                        }
                    }
                    oAdminLogin =  new DB_adminlogin();
                  
                

                    if (this.hdLoginUser.Value != this.txtUserName.Text.Trim())
                    {
                        int iAdminId = theAdminLogin.GetIdByName(this.txtUserName.Text.Trim());
                        if (iAdminId > 0)
                        {
                            base.Response.Write("<script language='javascript'>alert('该用户名已存在,请重新输入用户名!');</script>");
                            return;
                        }
                        oAdminLogin.AdminUser = this.txtUserName.Text.Trim();
                        oAdminLogin.AdminPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.txtPassword.Text), "MD5");
                        iAdminId = theAdminLogin.Add(oAdminLogin);
                        oAdminLogin.AdminId = iAdminId;
                    }
                    else
                    {
                        int iAdminId = theAdminLogin.GetIdByName(this.txtUserName.Text.Trim());

                        if (iAdminId <= 0)
                        {
                            oAdminLogin.AdminUser = this.txtUserName.Text.Trim();
                            oAdminLogin.AdminPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.txtPassword.Text), "MD5");
                            iAdminId = theAdminLogin.Add(oAdminLogin);
                            oAdminLogin.AdminId = iAdminId;
                        }
                        else
                        {
                            oAdminLogin.AdminId = iAdminId;
                            oAdminLogin.AdminUser = this.txtUserName.Text.Trim();
                            oAdminLogin.AdminPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.txtPassword.Text), "MD5");
                            theAdminLogin.Update(oAdminLogin);
                        }
                    }
                    oCompany.AdminId = oAdminLogin.AdminId;
                    theCompany.Update(oCompany);
                }
                else
                {
                    tbCompany tempCompany = theCompany.GetModelByName(oCompany.Name);
                    if (tempCompany != null)
                    {
                        base.Response.Write("<script language='javascript'>alert('该公司名已存在,请重新输入公司名!');</script>");
                        return;
                    }
                    oAdminLogin = new DB_adminlogin();
                  
                    int iAdminId = theAdminLogin.GetIdByName(this.txtUserName.Text.Trim());
                    if (iAdminId > 0)
                    {
                        base.Response.Write("<script language='javascript'>alert('该用户名已存在,请重新输入用户名!');</script>");
                        return;
                    }
                    oAdminLogin.AdminUser = this.txtUserName.Text.Trim();
                    oAdminLogin.AdminPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(re.replace(this.txtPassword.Text), "MD5");
                    iAdminId = theAdminLogin.Add(oAdminLogin);

                    oCompany.AdminId = iAdminId;
                    theCompany.Add(oCompany);
                  
                }
                base.Response.Write("<script language='javascript'>alert('保存成功');window.location.href='companyList.aspx';</script>");
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message.ToString());
            }

        }
    }
}